import {
  orderDetails,
  orderPayment,
  orderQuery,
  orderCancel
} from '../../apis/user';
import {
  splicing,
  formatTime,
  handleDate
} from '../../utils/util';
const app = getApp();

Page({
  data: {
    orderDetails: {}
  },

  onLoad(options) {
    this.setData({
      ...options
    });
  },

  onShow() {
    this.getOrderDetails(this.data.orderNo);
  },

  getOrderDetails(orderNo) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    orderDetails(orderNo).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        if (data.orderStatus == 0) {
          wx.setNavigationBarTitle({
            title: '已取消'
          });
        } else if (data.orderStatus == 1) {
          wx.setNavigationBarTitle({
            title: '待审核'
          });
        } else if (data.orderStatus == 2) {
          wx.setNavigationBarTitle({
            title: '审核失败'
          });
        } else if (data.orderStatus == 3 && data.payStatus == 0) {
          wx.setNavigationBarTitle({
            title: '待支付'
          });
        } else if (data.orderStatus == 3 && data.payStatus == 1) {
          wx.setNavigationBarTitle({
            title: '待参加'
          });
        } else if (data.orderStatus == 4) {
          wx.setNavigationBarTitle({
            title: '待参加'
          });
        } else if (data.orderStatus == 5) {
          wx.setNavigationBarTitle({
            title: '已完成'
          });
        } else if (data.orderStatus == 6) {
          wx.setNavigationBarTitle({
            title: '过期未核销'
          });
        };;
        data.createTime = formatTime(data.createTime);
        data.orderDetail.imageList = data.orderDetail.imageList.map(v => v = splicing(v));
        data.orderDetail.startTime = handleDate(data.orderDetail.startTime);
        data.orderDetail.closeTime = handleDate(data.orderDetail.closeTime);
        this.setData({
          orderDetails: data
        });
      };
    });
  },

  bottomAction() {
    if (this.data.orderDetails.orderStatus == 3 && this.data.orderDetails.payStatus == 0) {
      this.payment();
    } else if (this.data.orderDetails.orderStatus == 1) {
      wx.showModal({
        showCancel: true,
        title: '确定取消报名吗？',
        success: res => {
          if (res.confirm) {
            this.cancelSignUp();
          };
        }
      });
    } else if (this.data.orderDetails.orderStatus == 2) {
      wx.navigateTo({
        url: `/pages/editUserInfo/index?type=activity&from=order&activityId=${this.data.orderDetails.activityId}&orderNo=${this.data.orderDetails.orderNo}`
      });
    };
  },

  // 立即支付
  payment() {
    // wx.requestSubscribeMessage({
    //   tmplIds: ['_quuyfAlYNQwnbSdnyvgXveDHPNZ99V3h072l0jRhHs', 'pE5LxEcdYHS6B-TiE45j-GjiX-aV9e-KrHagJ1h4bbQ'],
    //   complete: res => {}
    // });
    wx.showLoading({
      title: '请稍候...',
    });
    orderPayment(this.data.orderNo).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        let {
          needPay,
          result
        } = res.data.data;
        if (needPay) {
          wx.requestPayment({
            nonceStr: result.nonceStr,
            package: result.packageValue,
            paySign: result.paySign,
            timeStamp: result.timeStamp,
            signType: result.signType,
            success: res => {
              this.queryOrder();
            },
            fail: res => {
              this.queryOrder();
            }
          });
        } else {
          this.queryOrder();
        };
      };
    });
  },
  // 支付查询
  queryOrder() {
    orderQuery(this.data.orderNo).then(res => {
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        if (data.isPay) {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            mask: true,
            duration: 1500
          });
          wx.setNavigationBarTitle({
            title: '已支付'
          });
        };
        this.getOrderDetails(this.data.orderNo);
      };
    });
  },

  // 取消报名
  cancelSignUp() {
    orderCancel(this.data.orderNo).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '取消成功',
          duration: 1000,
          icon: 'success',
          mask: true
        });
        this.setData({
          'orderDetails.orderStatus': 0
        });
        wx.setNavigationBarTitle({
          title: '已取消'
        });
      };
    });
  }
})